<template>
  <div class="login-container">
    <a-card class="login-card" title="用户登录">
      <a-tabs v-model:activeKey="activeKey">
        <a-tab-pane key="login" tab="登录">
          <a-form :model="loginForm" @finish="handleLogin">
            <a-form-item
              name="username"
              :rules="[{ required: true, message: '请输入用户名' }]"
            >
              <a-input v-model:value="loginForm.username" placeholder="用户名">
                <template #prefix>
                  <UserOutlined />
                </template>
              </a-input>
            </a-form-item>
            
            <a-form-item
              name="password"
              :rules="[{ required: true, message: '请输入密码' }]"
            >
              <a-input-password v-model:value="loginForm.password" placeholder="密码">
                <template #prefix>
                  <LockOutlined />
                </template>
              </a-input-password>
            </a-form-item>
            
            <a-form-item>
              <a-button type="primary" html-type="submit" class="login-button" :loading="loading">登录</a-button>
            </a-form-item>
            
            <div class="login-options">
              <a-checkbox v-model:checked="rememberMe">记住我</a-checkbox>
              <a class="forgot-link">忘记密码?</a>
            </div>
          </a-form>
        </a-tab-pane>
        
        <a-tab-pane key="register" tab="注册">
          <a-form :model="registerForm" @finish="handleRegister">
            <!-- 注册表单字段 -->
            <a-form-item
              name="username"
              :rules="[{ required: true, message: '请输入用户名' }]"
            >
              <a-input v-model:value="registerForm.username" placeholder="用户名"></a-input>
            </a-form-item>
            
            <a-form-item
              name="password"
              :rules="[{ required: true, message: '请输入密码' }]"
            >
              <a-input-password v-model:value="registerForm.password" placeholder="密码"></a-input-password>
            </a-form-item>
            
            <a-form-item
              name="email"
              :rules="[
                { required: true, message: '请输入邮箱' },
                { type: 'email', message: '请输入有效的邮箱地址' }
              ]"
            >
              <a-input v-model:value="registerForm.email" placeholder="邮箱"></a-input>
            </a-form-item>
            
            <a-form-item>
              <a-button type="primary" html-type="submit" class="login-button" :loading="registerLoading">注册</a-button>
            </a-form-item>
          </a-form>
        </a-tab-pane>
      </a-tabs>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue';
import { message } from 'ant-design-vue';
import { useRouter, useRoute } from 'vue-router';
import { useUserStore } from '@/stores/user';

const route = useRoute();
const router = useRouter();
const userStore = useUserStore();
const activeKey = ref('login');
const loading = ref(false);
const registerLoading = ref(false);
const rememberMe = ref(false);

const loginForm = ref({
  username: '',
  password: ''
});

const registerForm = ref({
  username: '',
  password: '',
  email: ''
});

const handleLogin = async () => {
  loading.value = true;
  try {
    await userStore.login(loginForm.value);
    message.success('登录成功');
    
    // 如果有重定向参数，跳转到相应页面
    const redirect = route.query.redirect as string;
    router.push(redirect || '/');
  } catch (error) {
    console.error('登录失败:', error);
    message.error('登录失败，请检查用户名和密码');
  } finally {
    loading.value = false;
  }
};

const handleRegister = async () => {
  registerLoading.value = true;
  try {
    // 这里需要调用注册API
    message.success('注册成功，请登录');
    activeKey.value = 'login';
  } catch (error) {
    console.error('注册失败:', error);
    message.error('注册失败');
  } finally {
    registerLoading.value = false;
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

.login-card {
  width: 360px;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.login-button {
  width: 100%;
}

.login-options {
  display: flex;
  justify-content: space-between;
  margin-top: -12px;
}

.forgot-link {
  color: #1890ff;
  cursor: pointer;
}
</style> 